#include "tim.h"
#include "gpio.h"

/* timer0 -> step1 Y*/
void MX_TIM0_Init(void)
{
    timer_parameter_struct timer0_init;
    timer_oc_parameter_struct timer0_ocintpara;

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_TIMER0);

    gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8); 

    timer_deinit(TIMER0);

    timer0_init.prescaler         = 2;
    timer0_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer0_init.counterdirection  = TIMER_COUNTER_UP;
    timer0_init.period            = 65535;
    timer0_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer0_init.repetitioncounter = 0;
    timer_init(TIMER0,&timer0_init);

    timer0_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer0_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer0_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer0_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer0_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer0_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_primary_output_config(TIMER0, ENABLE);
    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer0_ocintpara);

    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,1000);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    nvic_irq_enable(TIMER0_UP_IRQn, 1, 1);
    timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER0, TIMER_INT_UP);

    timer_auto_reload_shadow_disable(TIMER0);
    timer_enable(TIMER0);
}

/* timer1 -> step4 Mix*/
void MX_TIM1_Init(void)
{
    timer_parameter_struct timer1_init;
    timer_oc_parameter_struct timer1_ocintpara;

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_TIMER1);

    gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1); //STEP3

    timer_deinit(TIMER1);

    timer1_init.prescaler         = 2;
    timer1_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer1_init.counterdirection  = TIMER_COUNTER_UP;
    timer1_init.period            = 65535;
    timer1_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer1_init.repetitioncounter = 0;
    timer_init(TIMER1,&timer1_init);

    timer1_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer1_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer1_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer1_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer1_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer1_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER1,TIMER_CH_1,&timer1_ocintpara);

    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,1000);
    timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    nvic_irq_enable(TIMER1_IRQn, 1, 1);
    timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER1, TIMER_INT_UP);

    timer_auto_reload_shadow_disable(TIMER1);
}

/* TIMER2  -> step2 Z*/
void MX_TIM2_Init(void)
{
    timer_parameter_struct timer2_init;
    timer_oc_parameter_struct timer2_ocintpara;

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_TIMER2);

    gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6); //STEP3

    timer_deinit(TIMER2);

    timer2_init.prescaler         = 2;
    timer2_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer2_init.counterdirection  = TIMER_COUNTER_UP;
    timer2_init.period            = 65535;
    timer2_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer2_init.repetitioncounter = 0;
    timer_init(TIMER2,&timer2_init);

    timer2_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer2_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer2_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer2_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer2_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer2_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER2,TIMER_CH_0,&timer2_ocintpara);

    timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,1000);
    timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    nvic_irq_enable(TIMER2_IRQn, 1, 1);
    timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER2, TIMER_INT_UP);

    timer_auto_reload_shadow_disable(TIMER2);
}

/* timer3 -> step3 X*/
void MX_TIM3_Init(void)
{
    timer_parameter_struct timer3_init;
    timer_oc_parameter_struct timer3_ocintpara;

    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_TIMER3);

    gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8); //STEP3 PB7 PB8

    timer_deinit(TIMER3);

    timer3_init.prescaler         = 2;
    timer3_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer3_init.counterdirection  = TIMER_COUNTER_UP;
    timer3_init.period            = 65535;
    timer3_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer3_init.repetitioncounter = 0;
    timer_init(TIMER3,&timer3_init);

    timer3_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer3_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer3_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer3_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer3_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer3_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER3,TIMER_CH_2,&timer3_ocintpara);

    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_2,1000);
    timer_channel_output_mode_config(TIMER3,TIMER_CH_2,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

    nvic_irq_enable(TIMER3_IRQn, 1, 1);
    timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER3, TIMER_INT_UP);

    timer_auto_reload_shadow_disable(TIMER3);
}

void MX_TIM5_Init(void)
{
    timer_parameter_struct timer_init_struct;

    rcu_periph_clock_enable(RCU_TIMER5);

    timer_deinit(TIMER5);
    timer_init_struct.prescaler         = 120 - 1;
    timer_init_struct.period            = 65535;
    timer_init_struct.alignedmode       = TIMER_COUNTER_EDGE;
    timer_init_struct.counterdirection  = TIMER_COUNTER_UP;
    timer_init_struct.clockdivision     = TIMER_CKDIV_DIV1;
    timer_init_struct.repetitioncounter = 0;
    timer_init(TIMER5, &timer_init_struct);

    timer_enable(TIMER5);

}

/* systick */
void MX_TIM6_Init()
{
    rcu_periph_clock_enable(RCU_TIMER6);
    timer_parameter_struct timer6_init;

    timer_deinit(TIMER6);

    timer6_init.prescaler         = 120 - 1;
    timer6_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer6_init.counterdirection  = TIMER_COUNTER_UP;
    timer6_init.period            = 65535;
    timer6_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer6_init.repetitioncounter = 0;
    timer_init(TIMER6,&timer6_init);

    nvic_irq_enable(TIMER6_IRQn, 1, 1); 
    
    timer_interrupt_enable(TIMER6, TIMER_INT_UP);
    timer_enable(TIMER6);
}

